<?php
defined('IN_TS') or die('Access Denied.');

//小组首页

$jointype = intval($_GET['jointype']);
$isdonate = intval($_GET['isdonate']);

if($jointype==''){
	$jointype=1;
}

	$groupid = intval($_GET['groupid']);

if(!$groupid){
	$groupid = intval($_POST['groupid']);
	$lishi = intval($_POST['lishi']);
	$isvo = intval($_POST['isvo']);
}

if($jointype==3){
	$jointype=2;
}

$num = aac('group')->findCount('group_user','groupid='.$groupid.' and (isstock=1 or isstock=2)');

if(isMobile()){
	$userid = intval($_SESSION['tsuser']['userid']);
}else{
	$userid = aac('user')->isLogin();
}


if($_SESSION['tsuser']['follow_uid']){
	$follower = aac('user')->find('user_info',array('userid'=>$_SESSION['tsuser']['follow_uid']));
	$followText = '，是<a href="'.tsUrl('user','followed',array('id'=>$follower['userid'])).'">'.$follower['username'].'</a>把微澜推荐给ta的';
}


$strGroup = aac('group')->find('group',array(
	'groupid'=>$groupid
));

if(!$userid&&isMobile()){
	if($jointype==1){
		if($strGroup['isplace']==2){
			tsNotice('请将下面的二维码保存到相册，打开微信，在扫一扫中选择本地图片，这个网页仅支持在微信内访问！<br/><img src="https://ss2.meipian.me/users/5337276/fd8a1a50-e3ba-11ea-90d3-d954fe62f0f7.png">');
		}else{
			tsNotice('请将下面的二维码保存到相册，打开微信，在扫一扫中选择本地图片，这个网页仅支持在微信内访问！<br/><img src="https://ss2.meipian.me/users/5337276/0d2ec5a0-e3bb-11ea-90d3-d954fe62f0f7.png">');
		}
	}else{
		if($jointype==2){
			tsNotice('请将下面的二维码保存到相册，打开微信，在扫一扫中选择本地图片，这个网页仅支持在微信内访问！<br/><img src="https://ss2.meipian.me/users/5337276/06904890-e3bb-11ea-90d3-d954fe62f0f7.png">');
		}else{
			tsNotice('请将下面的二维码保存到相册，打开微信，在扫一扫中选择本地图片，这个网页仅支持在微信内访问！<br/><img src="https://ss2.meipian.me/users/5337276/19cf31a0-e3bb-11ea-90d3-d954fe62f0f7.png">');
		}
	}
}


$user = aac('user')->find('user_info',array('userid'=>$userid));
$groupuser=aac('group')->find('group_user','groupid='.$groupid.' and userid='.$userid);

if($jointype==1&&$strGroup['joinway']!=0||$jointype==2&&$strGroup['isdonate']==0){
	tsNotice("该馆已停止报名，请选择其他馆！",'点击前往',tsUrl('user','welcome'));
}

if($strGroup['status']==0){
		tsNotice("该馆已关闭，请选择其他馆！",'点击前往',tsUrl('user','welcome'));
}



if($jointype==1){
	$arr1=aac('group')->find('group_user',array('groupid'=>$groupid,'userid'=>$userid,'isvo'=>1));
	if($arr1){
		tsNotice("您已经是该馆馆员！");
	}
}
if($jointype==2){
	$arr2=aac('group')->find('group_user','groupid='.$groupid.' and userid='.$userid.' and isstock<>0');

	if($arr2){
		if($arr2['isstock']==1||$arr2['isstock']==2){
			tsNotice("您已经报名该馆馆东！");
		}else{

			if($strGroup['donateurl']){
				if($isdonate == 1){
					$url = tsUrl('group','newstock',array('groupid'=>$groupid));
				}else{
					$url =	$strGroup['donateurl'];
				}

			}else{
				$url = 'http://lxi.me/c03pr';
			}
			if($groupid==1){
				$text = '请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。完成月捐后，请加微信xingongmin2007，发送口令"总馆馆东"。';
			}else{
				$text =  $strGroup['groupname'].'已经有'.$num.'位馆东，';
				if($num < 20){
					$text = $text.'还差'.(20-$num).'人就可以达到举行馆东大会的要求了,';
				}else{
					$text = $text.'馆东越多，图书馆运营经费越充足，';
				}
				$text = $text.'请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。注意：完成月捐后，将出现提示页面，请扫客服二维码加微信，并发口令加群。';
			}

			tsNotice($text,'点击前往',$url,'true');
		}

	}
}

if(!$user['phone']||!$user['about']){
	header('Location: ' . tsUrl('my','setting',array('ts'=>'base','groupid'=>$groupid,'jointype'=>$jointype,'isvo'=>$isvo,'lishi'=>$lishi,'isdonate'=>$isdonate,'link'=>$link)));
}else{

	if(!$groupuser) {

		$arr = array(
			'userid' => $userid,
			'groupid' => $groupid,
			'addtime' => time(),
		);
		if ($jointype == 1) {
			$arr['isvo'] = 1;
			$arrUserInfo['isvo']=1;
		}
		if ($jointype == 2) {
			$arr['isstock'] = -1;
			if($isvo==1){
				$arr['isvo'] = 1;
				$arrUserInfo['isvo']=1;
			}
			if($lishi==1){
				$leadership=2;
				if($user['leadership']==1){
					$leadership = 3;
				}
				$arrUserInfo['leadership'] = $leadership;
				$arr['leadership']=2;
			}
		}
		$groupUserNum = aac('group')->findCount('group_user',array(
			'userid'=>$userid,
			'groupid'=>$groupid,
		));
		if($groupUserNum==0){
			aac('group')->create('group_user', $arr);
		}

		$arrUserInfo['groupids']=getGroupIds($userid);

		$count_group = aac('group')->findCount('group_user',array(
			'userid'=>$userid,
		));
		$arrUserInfo['count_group']=$count_group;
		aac('user')->update('user_info',array('userid'=>$userid),$arrUserInfo);
	}else{

		if($jointype==2){
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('isstock'=>-1));
		}else{
			aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('isvo'=>1));
		}

	}



	if($jointype==1){
		aac('user')->update('user_info',array('userid'=>$userid),array('isvo'=>1));
		if($strGroup['record']){
			$url =	$strGroup['record'];
		}else{
			$url = tsUrl('group','newvo',array('id'=>$groupid));
		}
		if(!$arr1){
			//发一条便笺
			$content = '报名做<a href="'.tsUrl('group','show',array('id'=>$groupid)).'">'.$strGroup['groupname'].'</a>馆员了'.$followText.'！';
			aac('weibo')->create('weibo',array(
				'userid'=>$TS_USER['userid'],
				'groupid'=>$groupid,
				'content'=>$content,
				'isaudit'=>0,
				'addtime'=>date('Y-m-d H:i:s'),
				'uptime'=>date('Y-m-d H:i:s'),
			));
			if($_SESSION['tsuser']['follow_uid']) {
				//发送系统消息开始
				$msg_userid = '0';
				$msg_touserid = $_SESSION['tsuser']['follow_uid'];
				$msg_content = '你的朋友'.$_SESSION['tsuser']['username'].'报名做<a href="'.tsUrl('group','show',array('id'=>$groupid)).'">'.$strGroup['groupname'].'</a>馆员了，ta是通过你的推荐了解微澜的，到目前为止，已经有'.$follower['count_followed'].'人通过你的转发来到了微澜，你干得漂亮，加油啊！';
				$msg_tourl = tsUrl('user', 'followed', array('id' => $_SESSION['tsuser']['follow_uid']));
				aac('message')->sendmsg($msg_userid, $msg_touserid, $msg_content, $msg_tourl);
				//发送系统消息end
			}

		}



	}else{
		if(!$arr2){
			//发一条便笺
			$content = '报名做<a href="'.tsUrl('group','show',array('id'=>$groupid)).'">'.$strGroup['groupname'].'</a>馆东了'.$followText.'！';
			aac('weibo')->create('weibo',array(
				'userid'=>$TS_USER['userid'],
				'groupid'=>$groupid,
				'content'=>$content,
				'isaudit'=>0,
				'addtime'=>date('Y-m-d H:i:s'),
				'uptime'=>date('Y-m-d H:i:s'),
			));

			if($_SESSION['tsuser']['follow_uid']) {
				//发送系统消息开始
				$msg_userid = '0';
				$msg_touserid = $_SESSION['tsuser']['follow_uid'];
				$msg_content = '你的朋友'.$_SESSION['tsuser']['username'].'报名做<a href="'.tsUrl('group','show',array('id'=>$groupid)).'">'.$strGroup['groupname'].'</a>馆东了，ta是通过你的推荐了解微澜的，到目前为止，已经有'.$follower['count_followed'].'人通过你的转发来到了微澜，你干得漂亮，加油啊！';
				$msg_tourl = tsUrl('user', 'followed', array('id' => $_SESSION['tsuser']['follow_uid']));
				aac('message')->sendmsg($msg_userid, $msg_touserid, $msg_content, $msg_tourl);
				//发送系统消息end
			}
		}


		if($strGroup['donateurl']){
			if($isdonate == 1){
				$url = tsUrl('group','newstock',array('groupid'=>$groupid));
			}else{
				$url =	$strGroup['donateurl'];
			}
		}else{
			$url = 'http://lxi.me/c03pr';
		}

	}
	if($url) {
		if ($jointype == 1){
			$text = '报名成功，即将进入欢迎页面，请务必仔细阅读欢迎信息。';

		}else{
			if($groupid==1){
				$text = '请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。完成月捐后，请加微信xingongmin2007，发送口令"总馆馆东"。';
			}else {

				$text = $strGroup['groupname'] . '已经有' . $num . '位馆东，';
				if ($num < 20) {
					$text = $text . '还差' . (20 - $num) . '人就可以达到举行馆东大会的要求了,';
				} else {
					$text = $text . '馆东越多，图书馆运营经费越充足，';
				}
				$text = $text . '请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。注意：完成月捐后，将出现提示页面，请扫客服二维码加微信，并发口令加群。';
			}
		}
		tsNotice($text, '点击前往', $url, 'true');

	}else{
		tsNotice("出错，请微信加xingongmin2007，向客服求助！");
	}

}




